package yunwo.cn.yzstation.adapter;

import android.animation.ValueAnimator;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.zhy.http.okhttp.callback.StringCallback;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import okhttp3.Call;
import yunwo.cn.yzstation.R;
import yunwo.cn.yzstation.api.HttpManager;
import yunwo.cn.yzstation.api.IassembleView;
import yunwo.cn.yzstation.api.okapi.GLCallBack;
import yunwo.cn.yzstation.base.BasicAdapter;
import yunwo.cn.yzstation.bean.CarListBean;
import yunwo.cn.yzstation.config.MyConstants;
import yunwo.cn.yzstation.view.activity.AssignmentActivity;
import yunwo.cn.yzstation.view.activity.CarsDetailsActivity;

/**
 * Created by lao on 2017/9/2.
 */

public class AssembleLvadapter extends BasicAdapter<CarListBean.DataBean.ListBean> {
    IassembleView mIassembleView;
    String type;
    public AssembleLvadapter(Context mctx ,IassembleView mIassembleView,String type) {
        super(mctx);
        this.mIassembleView = mIassembleView ;
        this.type = type;
    }

    @Override
    protected int setItemLayout() {
        return R.layout.lv_item_asseble;
    }

    @Override
    protected void bindData(View convertView, final CarListBean.DataBean.ListBean data, int position) {
        TextView next = get(convertView, R.id.next);


        TextView Continue = get(convertView, R.id.Continue);
        RelativeLayout autoRL = get(convertView, R.id.autoRL);
        TextView batch = get(convertView, R.id.batch);
        TextView state = get(convertView, R.id.state);
        TextView Logistics = get(convertView, R.id.Logistics);
        TextView carnumberandname = get(convertView, R.id.carnumberandname);
        TextView cartype = get(convertView, R.id.cartype);
        TextView dan = get(convertView, R.id.dan);
        TextView huowu = get(convertView, R.id.huowu);
        TextView depart = get(convertView, R.id.depart);
        Logistics.setText(data.getTruckTarget());
        batch.setText(data.getBatchNumber());
        autoRL.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mctx, CarsDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("depart", data);
                intent.putExtras(bundle);
                mctx.startActivity(intent);
            }
        });
        Continue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mctx, AssignmentActivity.class);
                intent.putExtra("Continue", data.getDepartId());
                intent.putExtra("type", type);
                mctx.startActivity(intent);

            }
        });
        depart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Map<String, Object> map = new HashMap<>();
                map.put("departId", data.getDepartId());
                HttpManager.hpptPostString3(MyConstants.depart,map).execute(new GLCallBack() {
                    @Override
                    public void onResponseT(String result, int code, String msg, int id) {
                        if (code == 200) {
                            Log.d("eeeeeeeeee",msg);
                            mIassembleView.departSueccss();
                        }else {
                            Toast.makeText(mctx, msg, Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Toast.makeText(mctx, "发车出错了", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        switch (data.getStatus()) {
            case 1:
                state.setText("待发车");
                break;
            case 2:
                state.setText("运输中");
                Continue.setEnabled(false);
                depart.setEnabled(false);
                Continue.setBackgroundResource(R.drawable.btn_unclick);
                depart.setBackgroundResource(R.drawable.btn_unclick);
                break;
            case 3:
                Continue.setEnabled(false);
                depart.setEnabled(false);
                Continue.setBackgroundResource(R.drawable.btn_unclick);
                depart.setBackgroundResource(R.drawable.btn_unclick);
                state.setText("已到达");
                break;
            case 4:
                Continue.setEnabled(false);
                depart.setEnabled(false);
                Continue.setBackgroundResource(R.drawable.btn_unclick);
                depart.setBackgroundResource(R.drawable.btn_unclick);
                state.setText("部分到货");
                break;
            case 5:
                Continue.setEnabled(false);
                depart.setEnabled(false);
                Continue.setBackgroundResource(R.drawable.btn_unclick);
                depart.setBackgroundResource(R.drawable.btn_unclick);
                state.setText("全部到货");
                break;
        }
        carnumberandname.setText(data.getPlateNumber() + "(" + data.getDriverName() + ")");
        cartype.setText(data.getVehicleType());
        dan.setText(data.getVotes()+"票");
        huowu.setText(data.getFactGoodNumber() + "/" + data.getGoodsNumber() + "件货物");

    }





    public static String getKey(HashMap<String,String> map, String value){
        String key = null;
        //Map,HashMap并没有实现Iteratable接口.不能用于增强for循环.
        for(String getKey: map.keySet()){
            if(map.get(getKey).equals(value)){
                key = getKey;
            }
        }
        return key;
        //这个key肯定是最后一个满足该条件的key.
    }
}
